package com.commonlib.downloadmgr.subclass;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import com.commonlib.downloadmgr.R;
import com.commonlib.downloadmgr.app.Application;
import com.commonlib.downloadmgr.base.DownloadBaseHelper;
import com.commonlib.downloadmgr.base.DownloadBaseJob;
import com.commonlib.downloadmgr.base.DownloadBaseManager;
import com.commonlib.downloadmgr.entity.Entity;
import com.commonlib.downloadmgr.subclass.DownloadAdapter.ViewHolder;

public class DownloadJob extends DownloadBaseJob<Entity>{
	/**
	 * 
	 */
	private static final long serialVersionUID = -886761659934183578L;
	private static final String TAG = DownloadJob.class.getSimpleName();
	
	private static final int UPDATE_INTERVAL = 100;
	private long lastUpdateUiTime;
	
	/**
	 * must have default constructor and visible
	 */
	public DownloadJob(){
		super();
	}
	
	public DownloadJob(Context context, DownloadBaseHelper<Entity> mHelper,
			Entity entity, String id, String url) {
		super(context, mHelper, entity, id, url);
	}

	@Override
	public DownloadBaseManager<Entity> getDownloadManager() {
		return DownloadManager.getInstance(Application.self.getApplicationContext());
	}

	@Override
	public void postUiUpdate(boolean forceUpdate) {
		
		if (container != null) {
			long interval = System.currentTimeMillis() - lastUpdateUiTime;
			if (interval >= UPDATE_INTERVAL || forceUpdate) {
				lastUpdateUiTime = System.currentTimeMillis();
				Handler mHandler = new Handler(Looper.getMainLooper());
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						View convertView = null;
						ViewHolder holder = null;
						if (container != null) {
							convertView = container.findViewWithTag(DownloadAdapter.PREFIX_CONVERTVIEW + job_id);
						}
						
						if (convertView != null) {
							holder = (ViewHolder) convertView.getTag(R.id.tag_id_convert_holder);
						}
						if (holder != null) {
							holder.tvTitle.setText(entity.title);
							holder.tvSize.setText(DownloadBaseHelper.formatSizeUnit(job_mTotalSize));
							switch (job_state) {
							case DownloadBaseJob.STATE_IDLE:
								holder.bnSwitch.setVisibility(View.INVISIBLE);
								holder.pbDownloading.setIndeterminate(true);
								holder.tvPercentage.setVisibility(View.INVISIBLE);
								break;
							case DownloadBaseJob.STATE_DOWNLOADING:
							case DownloadBaseJob.STATE_PAUSED:
							case DownloadBaseJob.STATE_SUCCESS:
							case DownloadBaseJob.STATE_FAILED:
								holder.bnSwitch.setVisibility(View.VISIBLE);
								holder.bnSwitch.setText(DownloadAdapter.getSwitchString(context, DownloadJob.this));
								
								holder.tvPercentage.setVisibility(View.VISIBLE);
								
								holder.pbDownloading.setIndeterminate(false);
								holder.pbDownloading.setMax(100);
								holder.pbDownloading.setProgress(mProgress);
								if (job_state == DownloadBaseJob.STATE_FAILED) {
									holder.tvPercentage.setTextColor(Color.RED);
									holder.tvPercentage.setText(DownloadAdapter.getErrorMsg(errorCode));
								}else{
									holder.tvPercentage.setTextColor(Color.WHITE);
									holder.tvPercentage.setText(mProgress + "%");
								}
								break;
							default:
								Log.e(TAG, "wrong state " + job_state);
								break;
							}
						}
					}
				});
			}
		}
		
	}

}
